package com.xc.stock.utils;

import javax.persistence.EntityManager;

import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.querydsl.jpa.impl.JPAUpdateClause;

/**
 * 
 * title: DslUtils.java 
 * Querydsl Sql工具类
 *
 * @author rplees
 * @email rplees.i.ly@gmail.com
 * @version 1.0   
 * @created Mar 3, 2020 5:28:40 PM
 */
public class DslUtils {
	static EntityManager entityManager;
	
	static EntityManager entityManager() {
		if(entityManager == null) {
			entityManager = SpringUtils.getBean(EntityManager.class);
		}
		
		return entityManager;
	}
	
	public static JPAQueryFactory getJPAQueryFactory() {
		return new JPAQueryFactory(entityManager());
	}
	
	public static JPAUpdateClause update(EntityPath<?> path) {
		return getJPAQueryFactory().update(path);
	}
	
	public static BooleanExpression whereAnd(BooleanExpression... predicate) {
		BooleanExpression where = predicate[0];
		
		for (int i = 1; i < predicate.length; i++) {
			where = where.and(predicate[i]);
		}
		
		return where;
	}

	public static String p(Path<?> path) {
		return path.getMetadata().getElement().toString();
	}
}
